package com.system.travel.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.system.travel.modules.system.entity.SysPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统权限Mapper接口
 *
 * @author lucas
 * @date 2024/01/01
 */
public interface SysPermissionMapper extends BaseMapper<SysPermission> {

    /**
     * 通过角色ID查询权限
     */
    @Select("select p.* from sys_permission p " +
            "left join sys_role_permission rp on p.id = rp.permission_id " +
            "where rp.role_id = #{roleId}")
    List<SysPermission> selectPermissionsByRoleId(@Param("roleId") Long roleId);

    /**
     * 查询用户的所有权限
     */
    @Select("SELECT DISTINCT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "LEFT JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1 AND p.del_flag = 0")
    List<SysPermission> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 查询菜单列表
     */
    @Select("select * from sys_permission where type = 1 and status = 1 and del_flag = 0 order by sort")
    List<SysPermission> selectMenuList();
} 